/**
 * Created by xuweijie on 2018/1/7.
 */
//注意：导航 依赖 element 模块，否则无法进行功能性操作

layui.use(['element','layer', 'form'], function(){
    var layer,form,element;
    layer = layui.layer;
    form = layui.form;
    element= layui.element;

    $(function () {
        $("a[name$='roleInfo']").click(function () {
            var roleInfo = JSON.parse($(this).attr('roleInfo'));
            var myContent = $('#updateRole');
            console.log(roleInfo);
            var form = layui.form;
            layui.use('layer',function () {
                layer.open({
                    type: 1,
                    skin:'layui-layer-molv',
                    title:  ['角色更新', 'font-size:18px;'],
                    closeBtn: 1,
                    shade: 0,
                    content:myContent,
                    area:  ['100%', '100%'],
                    shadeClose:true,
                    resize:true,
                    anim:1,
                    id: 'LAY_layuirole', //设定一个id，防止重复弹出
                    moveType: 1,
                    maxmin: false,
                    success:function (layero, index) {
                        //页面打开成功后操作
                        $('#roleName').val(roleInfo.role);
                        if (roleInfo.raRoots != null && roleInfo.raRoots.length > 0) {
                            roleInfo.raRoots.forEach(function (val) {
                                $('input:checkbox[name="root"][value="' + val.id + '"]').prop('checked', true);
                            });
                            form.render();
                        }

                        $('#resetbtn').click(function () {
                            var flag = false;
                            $('input[name="root"]:checked').each(function(){
                                // console.log($(this).val());
                                flag = true;
                            });
                            if (flag) {
                                $("input:checkbox[name=root]").prop('checked', false);
                                form.render();
                            } else {
                                layer.msg("当前未选择权限")
                            }
                        });

                        form.on('submit(confirmRole)', function(data){
                            var check_value =[];
                            $('input[name="root"]:checked').each(function(){
                                check_value.push($(this).val());
                            });
                            var params=$.param({
                                roleId:roleInfo.id,
                                rootId:check_value
                            },true);
                            $.ajax({
                                url:'/admin/role/updateRole',
                                type:'post',
                                data:params,
                                dataType:'json',
                                success:function (e) {
                                    layer.msg(e.msg);
                                    if (e.msg == "操作成功")
                                        setTimeout(function () {
                                            window.location.href="/admin/role/rolelist"
                                        },2000);
                                }
                            });
                            return false;
                        });
                    },
                    cancel:function (index, layero) {
                        //页面关闭后操作
                        $("input:checkbox[name=root]").prop('checked', false);
                        form.render();
                    }
                });
            });
        });

        //删除
        $("a[name$='deleteRole']").click(function () {
            var roleId=$(this).attr('roleId');
            layer.open({
                type: 1,
                title: false,
                closeBtn: 2,
                resize:true,
                content:'<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">' +
                '(涉及权限,请慎重)是否需要删除?删除后将不可恢复</div>',
                area:  '450px',
                shade: 0.8,
                shadeClose:true,
                anim:1,
                id: 'LAY_layuipro5', //设定一个id，防止重复弹出
                btn: ['绝不后悔', '我怂了'],
                moveType: 1,
                yes:function (index, layero) {
                    var params=$.param({
                        roleId:roleId
                    },true);
                    $.ajax({
                        url:'/admin/role/deleteRole',
                        type:'post',
                        data:params,
                        dataType:'json',
                        success:function (e) {
                            layer.msg(e.msg);
                            layer.close(index);
                            if (e.msg == "删除成功")
                                setTimeout(function () {
                                    window.location.href="/admin/role/rolelist"
                                },2000);
                        }
                    })
                }
            })
        });

        $("a[name$='addRole']").click(function () {
            var content = $('#addRoleInfo');
            var form = layui.form;
            layui.use('layer',function () {
                layer.open({
                    type: 1,
                    skin:'layui-layer-molv',
                    title:  ['添加新角色', 'font-size:18px;'],
                    closeBtn: 1,
                    shade: 0,
                    content:content,
                    area:  ['100%', '100%'],
                    shadeClose:true,
                    resize:true,
                    anim:1,
                    id: 'LAY_layuirole98', //设定一个id，防止重复弹出
                    moveType: 1,
                    maxmin: false,
                    success:function (layero, index) {
                        form.on('submit(confirmAdd)', function(data){
                            var check_value =[];
                            $('input[name="myRoot"]:checked').each(function(){
                                check_value.push($(this).val());
                            });
                            var roleName = $('#addRoleName').val();
                            var desc = $('#addRoleDesc').val();
                            var params=$.param({
                                roleName:roleName,
                                desc:desc,
                                rootId:check_value
                            },true);
                            $.ajax({
                                url:'/admin/role/addRole',
                                type:'post',
                                data:params,
                                dataType:'json',
                                success:function (e) {
                                    layer.msg(e.msg);
                                    if (e.msg == "添加成功")
                                        setTimeout(function () {
                                            window.location.href="/admin/role/rolelist"
                                        },2000);
                                }
                            });
                            return false;
                        });
                    },
                    cancel:function (index, layero) {
                        //页面关闭后操作
                    }
                });
            });
        });

        $("a[name$='addRoot']").click(function () {
            var content = $('#addRootInfo');
            var form = layui.form;
            layui.use('layer',function () {
                layer.open({
                    type: 1,
                    skin:'layui-layer-molv',
                    title:  ['添加新权限', 'font-size:18px;'],
                    closeBtn: 1,
                    shade: 0.3,
                    content:content,
                    area:  ['50%', '50%'],
                    shadeClose:true,
                    resize:true,
                    anim:1,
                    id: 'LAY_layuirole13', //设定一个id，防止重复弹出
                    moveType: 1,
                    maxmin: false,
                    success:function (layero, index) {
                        $('#resetRoot').click(function () {
                            $('#rootName').val('');
                            $('#rootDesc').val('');
                        });
                        $('#confirmAddRoot').click(function () {
                            var rootName = $('#rootName').val();
                            var desc = $('#rootDesc').val();
                            if (rootName == '' || desc == '')
                                layer.msg('请完善输入信息，不允许为空');
                            else {
                                var params=$.param({
                                    rootName:rootName,
                                    desc:desc
                                },true);
                                $.ajax({
                                    url:'/admin/role/addRoot',
                                    type:'post',
                                    data:params,
                                    dataType:'json',
                                    success:function (e) {
                                        layer.msg(e.msg);
                                        if (e.msg == "添加成功") {
                                            setTimeout(function () {
                                                window.location.href="/admin/role/rolelist"
                                            },2000);
                                        }
                                    }
                                });
                            }
                        });
                    },
                    cancel:function (index, layero) {
                        $('#rootName').val('');
                        $('#rootDesc').val('');
                    }
                });
            });
        });

        $('.rootInfo').click(function () {
            var rootId = $(this).attr('id');
            var rootName = $(this).attr('rootName');
            var rootDesc = $(this).text();
            var content = $('#addRootInfo');
            layui.use('layer',function () {
                layer.msg('点击以下选项进行操作，涉及权限，请慎重选择', {
                    time: 10000, //10s后自动关闭
                    btn: ['修改', '删除'],
                    shade: 0.3,
                    shadeClose:true,
                    id: 'LAY_layuirole00',
                    btn1:function () {
                        layer.close(layer.msg());
                        $('#fieldTitle').text('更新权限');
                        $('.cbtn').text('确认更新');
                        layui.use('layer',function () {
                            layer.open({
                                type: 1,
                                skin:'layui-layer-molv',
                                title:  ['修改权限', 'font-size:18px;'],
                                closeBtn: 1,
                                shade: 0.3,
                                content:content,
                                area:  ['50%', '50%'],
                                shadeClose:true,
                                resize:true,
                                anim:1,
                                id: 'LAY_layuirole33', //设定一个id，防止重复弹出
                                moveType: 1,
                                maxmin: false,
                                success:function (layero, index) {
                                    $('#rootName').val(rootName);
                                    $('#rootDesc').val(rootDesc);
                                    $('#resetRoot').click(function () {
                                        $('#rootName').val('');
                                        $('#rootDesc').val('');
                                    });
                                    $('#confirmAddRoot').click(function () {
                                        var newRootName = $('#rootName').val();
                                        var newDesc = $('#rootDesc').val();
                                        if (rootName == newRootName && rootDesc == newDesc)
                                            layer.msg('未作任何修改');
                                        else if (newRootName == '' || newDesc == '')
                                            layer.msg('请完善输入信息，不允许为空');
                                        else {
                                            var params=$.param({
                                                id:rootId,
                                                root:newRootName,
                                                descript:newDesc
                                            },true);
                                            $.ajax({
                                                url:'/admin/role/updateRoot',
                                                type:'post',
                                                data:params,
                                                dataType:'json',
                                                success:function (e) {
                                                    layer.msg(e.msg);
                                                    if (e.msg == "更新成功")
                                                        setTimeout(function () {
                                                            window.location.href="/admin/role/rolelist"
                                                        },2000);
                                                }
                                            });
                                        }
                                    });
                                },
                                cancel:function (index, layero) {
                                    $('#rootName').val('');
                                    $('#rootDesc').val('');
                                }
                            });
                        });
                    },
                    btn2:function () {
                        var params=$.param({
                            id:rootId
                        },true);
                        $.ajax({
                            url:'/admin/role/deleteRoot',
                            type:'post',
                            data:params,
                            dataType:'json',
                            success:function (e) {
                                layer.msg(e.msg);
                                if (e.msg == "删除成功")
                                    setTimeout(function () {
                                        window.location.href="/admin/role/rolelist"
                                    },2000);
                            }
                        });
                    }
                });
            });
        })
    });

});

